﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using log4net;

using SSATool.Common.Logging.Properties;

#endregion

namespace SSATool.Common.Logging
{
    /// <summary>
    /// Contains the a time base logging functionality. You should use
    /// this class in conjunction with the using() method.
    /// </summary>
    /// <typeparam name="T">Specifies the logging category.</typeparam>
    public class TimeLogger<T> : IDisposable
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        
        private readonly DateTime _startTime;
        private readonly string _sectionName;

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new TimeLogger instance.
        /// </summary>
        public TimeLogger(string sectionName)
        {
            if (Logger<T>.IsInfoEnabled)
            {
                _startTime = DateTime.Now;
                _sectionName = (sectionName ?? string.Empty);
            }
        }

        /// <summary>
        /// Creates a new TimeLogger instance.
        /// </summary>
        public TimeLogger()
            : this(string.Empty)
        {
        }

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Performs application-defined tasks associated with freeing, 
        /// eleasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (Logger<T>.IsInfoEnabled)
                Logger<T>.InfoFormat(Resources.LogTimeMeasure, _sectionName, (DateTime.Now - _startTime).Milliseconds);
        }

        #endregion
    }
}
